package com.simen.hellobytes.socket;

import com.simen.hellobytes.IOUtil;
import com.simen.hellobytes.socket.helper.SocketClientListener;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * @author Created by Simen.
 * @date 创建日期 2018/5/9 13:57
 * @modify 修改者 Simen
 */
public class SocketClient extends AbstractSocketClient {

    private String serverIp;
    private int port;

    private byte[] messageBytes;

    private Socket socket = null;
    private OutputStream outputStream = null;
    private InputStream inputStream = null;

    private boolean isRunning = false;

    private SocketClientListener socketClientListener;

    public SocketClient(String serverIp, int port) {
        this.serverIp = serverIp;
        this.port = port;
    }

    public SocketClient(String serverIp, int port, byte[] messageBytes) {
        this.serverIp = serverIp;
        this.port = port;
        this.messageBytes = messageBytes;
    }

    public void sendMessage() {
        isRunning = true;

        socket = new Socket();
        try {
            socket.connect(new InetSocketAddress(serverIp, port), mConnectTimeout == -1 ? 0 : mConnectTimeout);

            socket.setReuseAddress(reuseAddress);

            if (mReadTimeout != -1) {
                socket.setSoTimeout(mReadTimeout);
            }

            if (recSocketBufSize != -1) {
                socket.setReceiveBufferSize(recSocketBufSize);
            }

            if (sendSocketBufSize != -1) {
                socket.setSendBufferSize(sendSocketBufSize);
            }

            inputStream = socket.getInputStream();
            outputStream = socket.getOutputStream();

            if (socketClientListener != null) {
                socketClientListener.onConnectReady(socket, inputStream, outputStream);
            }

            if (messageBytes != null) {
                outputStream.write(messageBytes);
            }

            if (receiveBackMsg) {
                byte[] buf = new byte[bufSize];

                recBytes = IOUtil.readStream(inputStream, buf);

                if (transform != null) {
                    new SocketClient(transform.getServerIp(), transform.getPort(), transform.transform(recBytes)).sendMessage();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeImmediately();
        }

        isRunning = false;
    }

    public void closeImmediately() {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
            }
            inputStream = null;
        }
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
            }
            outputStream = null;
        }
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
            }
            socket = null;
        }
    }

    public SocketClientListener getSocketClientListener() {
        return socketClientListener;
    }

    public void setSocketClientListener(SocketClientListener socketClientListener) {
        this.socketClientListener = socketClientListener;
    }

    public byte[] getMessageBytes() {
        return messageBytes;
    }

    public void setMessageBytes(byte[] messageBytes) {
        this.messageBytes = messageBytes;
    }

    public boolean isRunning() {
        return isRunning;
    }

    public void setRunning(boolean running) {
        isRunning = running;
    }


}
